package com.iwdm.renovation;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        db = new DBHelper(this).getWritableDatabase();
        final EditText usernameEditText = findViewById(R.id.username);
        final EditText passwordEditText = findViewById(R.id.password);
        findViewById(R.id.login).setOnClickListener(v ->
                login(usernameEditText.getText().toString(),
                        passwordEditText.getText().toString()));
        findViewById(R.id.btn_reg).setOnClickListener(v ->
                reg(usernameEditText.getText().toString(),
                        passwordEditText.getText().toString()));

    }

    private void reg(String str_name, String str_pwd) {
        if (TextUtils.isEmpty(str_name)) {
            showToast("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(str_pwd)) {
            showToast("请输入密码");
            return;
        }
        ContentValues values = new ContentValues();
        values.put("name", str_name);
        values.put("password", str_pwd);
        long temp = db.insert("user", null, values);
        // 判断 账号和密码
        if (temp != -1) {
            showToast("注册成功");
            commit(str_name, str_pwd);
        } else {
            showToast("注册失败");
        }
    }

    private void commit(String str_name, String str_pwd) {
        SharedPreferences sharedPreferences = getSharedPreferences("temp", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("name", str_name);
        editor.putString("pwd", str_pwd);
        editor.commit();
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }

    private void login(String account, String str_pwd) {
        if (TextUtils.isEmpty(account)) {
            showToast("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(str_pwd)) {
            showToast("请输入密码");
            return;
        }
        String pwd = getres(account);
        // 判断 账号和密码
        if (str_pwd.equals(pwd)) {
            commit(account, pwd);
        } else {
            showToast("登录失败！ 账号/密码错误");
        }
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    protected String getres(String string) {

        String temp = "";
        Cursor cursor = db.query("user", null, "name = ?", new String[]{string}, null, null, null, "1");
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            temp = cursor.getString(cursor.getColumnIndex("password"));
        } else {
            temp = "查询失败！";
        }
        // 关闭游标，释放资源
        cursor.close();
        return temp;

    }
}
